package com.freedom.admin.interfaces.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freedom.admin.application.service.cmd.RoleCmdService;
import com.freedom.admin.application.service.query.RoleQueryService;
import com.freedom.admin.infrastructure.persistence.pojo.form.RoleForm;
import com.freedom.admin.infrastructure.persistence.pojo.form.RoleResourceForm;
import com.freedom.admin.infrastructure.persistence.pojo.form.RoleStatusForm;
import com.freedom.admin.infrastructure.persistence.pojo.query.RolePageQuery;
import com.freedom.admin.infrastructure.persistence.pojo.vo.role.RolePageVO;
import com.freedom.admin.infrastructure.persistence.pojo.vo.role.RoleVO;
import com.freedom.common.core.base.BaseIntsForm;
import com.freedom.common.core.result.CommonResult;
import com.freedom.common.core.result.PageResult;
import com.freedom.common.framework.pojo.OptionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "「管理端」角色接口")
@RestController
@RequestMapping("/api/role")
@RequiredArgsConstructor
public class SysRoleController {

    private final RoleQueryService roleQueryService;

    private final RoleCmdService roleCmdService;

    @ApiOperation(value = "角色分页列表")
    @GetMapping("/pages")
    public PageResult<RolePageVO> listRolePages(RolePageQuery queryParams) {
        Page<RolePageVO> result = roleQueryService.listRolePages(queryParams);
        return PageResult.success(result);
    }

    @ApiOperation(value = "角色下拉列表")
    @GetMapping("/options")
    public CommonResult<List<OptionVO>> listRoleOptions() {
        List<OptionVO> list = roleQueryService.listRoleOptions();
        return CommonResult.success(list);
    }

    @ApiOperation(value = "角色详情")
    @GetMapping("/{roleId}")
    public CommonResult<RoleVO> getRoleDetail(@ApiParam("角色ID") @PathVariable Integer roleId) {
        RoleVO roleVO = roleQueryService.getRoleDetail(roleId);
        return CommonResult.success(roleVO);
    }

    @ApiOperation(value = "获取角色的资源ID集合", notes = "资源包括菜单和权限ID")
    @GetMapping("/{roleId}/resources")
    public CommonResult<RoleResourceForm> getRoleResources(@ApiParam("角色ID") @PathVariable Integer roleId) {
        RoleResourceForm resourceIds = roleQueryService.getRoleResources(roleId);
        return CommonResult.success(resourceIds);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping
    public CommonResult<Boolean> addRole(@RequestBody @Validated RoleForm roleForm) {
        boolean result = roleCmdService.addRole(roleForm);
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "修改角色")
    @PutMapping(value = "/{id}")
    public CommonResult<Boolean> modifyRole(@RequestBody @Validated RoleForm roleForm) {
        boolean result = roleCmdService.modifyRole(roleForm);
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/{ids}")
    public CommonResult<Boolean> removeRoles(@RequestBody @Validated BaseIntsForm baseIntsForm) {
        boolean result = roleCmdService.removeRoles(baseIntsForm.getIds());
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "修改角色状态")
    @PutMapping(value = "/status")
    public CommonResult<Boolean> modifyRoleStatus(@RequestBody @Validated RoleStatusForm roleStatusForm) {
        boolean result = roleCmdService.modifyRoleStatus(roleStatusForm);
        return CommonResult.judge(result);
    }

    @ApiOperation(value = "分配角色的资源权限")
    @PutMapping("/resources")
    public CommonResult<Boolean> modifyRoleResource(@RequestBody @Validated RoleResourceForm roleResourceForm) {
        boolean result = roleCmdService.modifyRoleResource(roleResourceForm);
        return CommonResult.judge(result);
    }
}
